package servlet;

import bean.Result;
import biz.EmployeeBiz;
import biz.userBiz;
import utils.Util;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ModifyPwdServlet", value = "/ModifyPwdServlet.s")
public class ModifyPwdServlet extends HttpServlet {
    private EmployeeBiz biz = new EmployeeBiz();
    private userBiz userBiz = new userBiz();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Result result = new Result(0,"该邮箱用户未注册!",null);
        // 先判断是用户还是员工
        try {
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            HttpSession session = request.getSession();
            String email = (String) session.getAttribute("email");
            // 1 表示存在
            if (userBiz.exists(name, email) == 1){
                userBiz.modify(name, password, email);
                result = new Result(1, "用户密码修改成功!", "login/Login-M.html");
            }
            if (biz.existsEmp(name, email) == 1){
                biz.modifyPwd(name, password, email);
                result = new Result(1, "员工密码修改成功!", "background/backLogin.html");
            }
        }catch (Exception e){
            e.printStackTrace();
            result = new Result(0, "用户名或密码错误",null);
        }

        Util.WriteJson(result, response);
    }


}
